/**
  * 系统token保存
 */
import Cookies from 'js-cookie'
/**
 * 登录的token 名称
 * @type {string}
 */
const userIdKey = 'userId'

const Token = 'accessToken'

/**
 * userId
 * @returns {String} token
 */
export function getUserId () {
  return Cookies.get(userIdKey)
}

/**
 * 设置userId
 * @param {String} userId userId
 * @returns {Void} void
 */
export function setUserId (userId) {
  return Cookies.set(userIdKey, userId)
}

/**
 * 移除userId
 * @returns {Void} void
 */
export function removeuserId () {
  return Cookies.remove(userIdKey)
}

/**
 * Token
 * @returns {String} token
 */
export function getToken () {
  return Cookies.get(Token)
}

/**
 * 设置AccessToken
 * @param {String} Token Token
 * @returns {Void} void
 */
export function setToken (token) {
  return Cookies.set(Token, token)
}

/**
 * 移除AccessToken
 * @returns {Void} void
 */
export function removeToken () {
  return Cookies.remove(Token)
}

/**
 * 删除用户信息
 */

export function removeUser () {
  removeToken()
  removeuserId()
}

/**
 * 跳转到登录门户
 * @returns {Void} void
 */
export function redirctLogin () {
  // 登录成功后始终跳转到/index.html, 以确保非nginx部署时出现404的问题
  // 构造登录成功后的跳转地址
  let locationUri = window.location.origin
  if (locationUri === undefined || locationUri === null) {
    // 协议 + 主机（域名）
    locationUri = window.location.protocol + '//' + window.location.host
  }
  // window.open(process.env.LOGIN_URL + '?redirct=' + locationUri, '_self')
}
